package com.intexh.quickandroid.helper;

import android.content.Context;
import android.content.Intent;

import com.intexh.quickandroid.module.chat.ChatHelper;
import com.intexh.quickandroid.module.login.LoginActivity;
import com.intexh.quickandroid.utils.GsonUtils;
import com.intexh.quickandroid.utils.LogCatUtil;
import com.intexh.quickandroid.utils.Settings;
import com.intexh.quickandroid.utils.ValidateUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by FrankZhang on 2017/9/12.
 */

public class UserHelper extends BaseUserHelper {

    private static final String CURRENT_USER_INFO = "current_user_info";

    public static boolean checkGoLogin(Context context){
        if(!UserHelper.isLogin()){
            Intent intent = new Intent(context,LoginActivity.class);
            context.startActivity(intent);
//            DialogUtils.showDefaultDialog(context,"提示","请登录",new DialogUtils.DialogImpl(){
//                @Override
//                public void onOk() {
//
//                }
//            });
            return true;
        }else{
            return false;
        }
    }

    /**
     * 登录执行 发送登录成功消息
     * @param userBean
     */
    public static void login(UserBean userBean) {
            saveCurrentToken(userBean.getKey());
        saveCurrentUserToSP(userBean);
        EventBus.getDefault().post(new LoginStatusEvent(LoginStatusEvent.LoginStatus.LOGIN));
        ChatHelper.getInstance().login(userBean.getChat_id(),userBean.getChat_pwd());
        // 设置推送别名 等 是否需要请求一些接口
        //设置别名
//        JpushController.setAlias(MainApplication.getAppContext(),userBean.getPush_id());
    }

    /**
     * 退出登录执行 发送退出消息
     */
    public static void loginOut() {
        clearCurrentToken();
        clearCurrentUserToSP();
        EventBus.getDefault().post(new LoginStatusEvent(LoginStatusEvent.LoginStatus.LOGINOUT));
        ChatHelper.getInstance().loginOut();
        // 取消别名 是否需要调到登录页
//       JpushController.setAlias(MainApplication.getAppContext(),"");
    }



    /**
     * 是否登录
     * @return true yes
     */
    public static boolean isLogin() {
        return  ValidateUtils.isValidate(getCurrentToken());
    }

    /**
     * 缓存登录用户信息
     * @param bean
     */
    public static void saveCurrentUserToSP(UserBean bean) {
        LogCatUtil.e("frank","保存的登录信息:  "+ GsonUtils.serializedToJson(bean));
        Settings.setString(CURRENT_USER_INFO, GsonUtils.serializedToJson(bean));
        //同步登录环信账号信息
        ChatHelper.getInstance().getUserInfo().setUid(bean.getUserid());
        ChatHelper.getInstance().getUserInfo().setAvatar(bean.getMember_avatar());
        ChatHelper.getInstance().getUserInfo().setDisplay_name(bean.getUsername());
        ChatHelper.getInstance().getUserInfo().setChat_id(bean.getChat_id());
        ChatHelper.getInstance().getUserInfo().setChat_pwd(bean.getChat_pwd());
    }

    public static void saveAvatar(String avatar) {
        UserBean user = getUser();
        user.setMember_avatar(avatar);
        Settings.setString(CURRENT_USER_INFO, GsonUtils.serializedToJson(user));
    }

    public static void saveUserName(String userName) {
        UserBean user = getUser();
        user.setUsername(userName);
        Settings.setString(CURRENT_USER_INFO, GsonUtils.serializedToJson(user));
    }
    public static void saveUserMobile(String phone) {
        UserBean user = getUser();
        user.setMember_mobile(phone);
        Settings.setString(CURRENT_USER_INFO, GsonUtils.serializedToJson(user));
    }
    public static void saveUserVipLevel(String level) {
        UserBean user = getUser();
        user.setMember_grade(level);
        Settings.setString(CURRENT_USER_INFO, GsonUtils.serializedToJson(user));
    }
    public static void saveUserMemberPoints(int member_points) {
        UserBean user = getUser();
        user.setMember_points(member_points);
        Settings.setString(CURRENT_USER_INFO, GsonUtils.serializedToJson(user));
    }




    /**
     * 获取缓存登录用户信息
     */
    public static UserBean getUser() {
        UserBean userBean = GsonUtils.jsonToBean(Settings.getString(CURRENT_USER_INFO, ""), UserBean.class);
        return userBean==null?new UserBean():userBean;
    }

    /**
     * 清除缓存登录用户信息
     */
    public static void clearCurrentUserToSP() {
        Settings.setString(CURRENT_USER_INFO,"");
    }
}
